package cn.yo2.irain.log4money.app.msgboard;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.support.PagedListHolder;

import com.opensymphony.xwork2.ActionSupport;
import cn.yo2.irain.common.util.CMyDateTime;
import cn.yo2.irain.common.util.Page;

//import cn.yo2.irain.common.util.CMyDateTime;
//import cn.yo2.irain.common.util.Page;

public class MessagesBoardAction extends ActionSupport implements SessionAware, ServletRequestAware {

    
    private Map session;

    private HttpServletRequest request;
    private HttpSession httpSession;

    private IMessagesBoardServies messagesBoardServices;

    private List<MessagesBoard> pagelist = null;
    private MessagesBoard mb;

    private String mbid;
    private String mbreplycontent;

    private int cpage = 1;
    private int total = 0;
    private String url = "list.action";


    public String execute(){

        List alllist = this.messagesBoardServices.findAll();
        PagedListHolder holder = new PagedListHolder(alllist);

        holder.setPage(cpage - 1);
        holder.setPageSize(Page.pageSize);

        total = Page.getTotal(alllist.size());
        pagelist = holder.getPageList();
        
        mb = new MessagesBoard();
        return SUCCESS;
    }

    public String save(){

        if(mbid != null && mbid.length() > 0){
            mb = messagesBoardServices.findById(mbid);
            mb.setReplytime(CMyDateTime.now().toString("yyyy-MM-dd HH:MM"));
            mb.setReplyContent(mbreplycontent);
            messagesBoardServices.save(mb);
        }else{
            httpSession = request.getSession();
            String loginName = (String)httpSession.getAttribute("login_name");

            mb.setCrtime(CMyDateTime.now().toString("yyyy-MM-dd HH:MM"));
            mb.setCruser(loginName);
            messagesBoardServices.save(mb);
        }

        return SUCCESS;
    }

    public String delete(){

        mb = messagesBoardServices.findById(mbid);
        messagesBoardServices.delete(mb);
        return SUCCESS;
    }

    public void setSession(Map session) {
        this.session = session;
    }

    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    public IMessagesBoardServies getMessagesBoardServices() {
        return messagesBoardServices;
    }

    public void setMessagesBoardServices(IMessagesBoardServies messagesBoardServices) {
        this.messagesBoardServices = messagesBoardServices;
    }

    public MessagesBoard getMb() {
        return mb;
    }

    public void setMb(MessagesBoard mb) {
        this.mb = mb;
    }

    public List<MessagesBoard> getPagelist() {
        return pagelist;
    }

    public void setPagelist(List<MessagesBoard> pagelist) {
        this.pagelist = pagelist;
    }

    public int getCpage() {
        return cpage;
    }

    public void setCpage(int cpage) {
        this.cpage = cpage;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getMbid() {
        return mbid;
    }

    public void setMbid(String mbid) {
        this.mbid = mbid;
    }

    public String getMbreplycontent() {
        return mbreplycontent;
    }

    public void setMbreplycontent(String mbreplycontent) {
        this.mbreplycontent = mbreplycontent;
    }
}